$(function(){
	var currentUrl = window.location.href;
	var id = location.search.match(new RegExp("[\?\&]id=([^\&]+)", "i"))[1];
	/*var cache = {};
	cache.id = id;*/
	var  ttt = id;
	queryToday(id);
	function queryToday(id){
	var p ={id:id};
	$.ajax({
		  type:'POST',
		  url:'/movie.front/orderController/getOrderListById',
		  dataType:'json',
		  contentType:'application/json',
		  data:JSON.stringify(p),
		  success:function(data){
			$('#myCurrentOrder').empty();
    $('#myCurrentOrder').append("<thead><tr  ><td></td><td>订单编号</td><td>订单名称</td><td>价格</td><td>时间</td><td>影厅</td><td>座位号</td></tr></thead>")
			 var array = data.result;
    for(var i=0;i<array.length;i++){
        var html="";
       html= html+'<tbody><tr><td><input type="checkbox" id='+array[i].id+'></td><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+transfer(array[i].orderTime)+'</td><td>'+array[i].hallId+'</td><td>'+array[i].orderSeats+'</td></tr></tbody>'
      $('#myCurrentOrder').append(html);  
      
    }
   /* $('#myCurrentOrder').append(' <button id="btn-reset-ticket" style="margin-top:60px;width:200px;height:40px;margin-left:60px;background:#f87207;border:none">退票</button>')			*/
		  }
	});
	}
	var p ={id:id};
	$.ajax({
		  type:'POST',
		  url:'/movie.front/orderController/getHistoryOrderListById',
		  dataType:'json',
		  contentType:'application/json',
		  data:JSON.stringify(p),
		  success:function(data){
			$('#historyTable').empty();
  $('#historyTable').append("<thead><tr><td>订单编号</td><td>订单名称</td><td>价格</td><td>时间</td><td>影厅</td><td>座位号</td></tr></thead>")
			 var array = data.result;
  for(var i=0;i<array.length;i++){
      var html="";
    var orderTime =  transfer(array[i].orderTime);
     html= html+'<tbody><tr><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+orderTime+'</td><td>'+array[i].hallId+'</td><td>'+array[i].orderSeats+'</td></tr></tbody>'
    $('#historyTable').append(html); 
  }
      			
		  }
	});
	
	$('#btn-reset-ticket').click(function(){
		//选择id
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			
			  var id = {id:id};
			updateReturnTicket(id);
		     }
	//	alert("aaaaaaa");
	});

	  function updateReturnTicket(id){
		 
		  $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/updateReturnTicket",
			  data:JSON.stringify(id),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
		        var result = data.result;
		          if(result>0){
		        	  alert("退票成功");
		        	 
		        	  queryToday(ttt);
		          }else{
		        	  alert("退票失败");
		          }
			     }
		   });
	  }
	  
	
});

function transfer(val){
	if(val){
		val = parseInt(val, 10) * 1000;
		var date = new Date(val);
		//return date.format('yyyy-MM-dd hh:mm:ss');
		return date.format('yyyy-MM-dd hh:mm:00');
	}
	return '';
}
//重写javaScript日期对象原型
Date.prototype.format = function(fmt){
	var o = {
		'M+': this.getMonth() + 1,//月份
		'd+': this.getDate(),//日
		'h+': this.getHours(),//小时
		'm+': this.getMinutes(),//分
		's+': this.getSeconds(),//秒
		'q+': Math.floor((this.getMonth() + 3) / 3),//季度
		'S': this.getMilliseconds()//毫秒
	};
	if(/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
	for( var k in o)
		if(new RegExp('(' + k + ')').test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
	return fmt;
}

